/*
	用户详情管理
	功能：
		用户详情查看
		用户信息修改
*/
//登录信息
var adminLoginedInfo = Cookies.getJSON('kplus_ad_logined_info');

var vipDict = {
	normal: '普通',
	silver: '銀星',
	gold: '金星',
	diamond: '鑽星'
}

var KPN = JSON.parse(sessionStorage.getItem('kplus_web_dict_pName'));
//dom 加载完成
$(function(){
	//ajax data
	var ajaxData = {
		uId: getParam('uId'),
		aId: adminLoginedInfo.aId,
		aToken: adminLoginedInfo.aToken
	}
	//获取dom 元素
	var email = $('input[name="email"]');
	var phone = $('input[name="phone"]');
	var username = $('input[name="username"]');
	var createTime = $('input[name="createTime"]');
	var userFacebook = $('input[name="userFacebook"]');
	var userWeChat = $('input[name="userWeChat"]');
	var userVipLever = $('input[name="userVipLever"]');
	var vipTime = $('input[name="vipTime"]');
	var userSex = $('input[name="userSex"]');
	var userBirthday = $('input[name="userBirthday"]');
	var lastLoginIp = $('input[name="lastLoginIp"]');
	var lastLoginTime = $('input[name="lastLoginTime"]');
	var amountPSN = $('input[name="amountPSN"]');
	var amountOrder = $('input[name="amountOrder"]');
	var dataSize = $('#dataSize');
	var useraddr = $('input[name="useraddr"]')

	//构建表单内容
	function createFormData(data){
		email.val(data.u_mail);
		phone.val(data.u_phone);
		username.val(data.u_name);
		createTime.val(formatTime(data.format_time));
		userFacebook.val(data.u_facebook);
		userWeChat.val(data.u_wechat);
		userVipLever.val(vipDict[data.vip_level]);
		vipTime.val(data.vip_start + '-' + data.vip_end);
		userSex.val(data.u_sex);
		userBirthday.val(data.u_birthday);
		lastLoginIp.val(data.last_login_ip);
		lastLoginTime.val(formatTime(data.last_login_time));
		dataSize.text(data.login_record.length);
		amountPSN.val(data.AOC.code_amount.tw);
		amountOrder.val(data.AOC.order_amount.tw);
		useraddr.val(data.u_addr);
		//登录记录
		var login_record_arr = new Array();
		for(var i = data.login_record.length - 1; i >= 0; i--){
			var obj = {
				UID: data.login_record[i].u_id,
				userName: data.u_name,
				userMail: data.u_mail,
				loginIp: data.login_record[i].u_ip,
				loginTime: formatTime(data.login_record[i].format_time)
			}
			login_record_arr.push(obj)
		}
		// 序列号激活记录
		var history_code_data_recode = new Array();
		for (var i = data.history_code_data.length - 1; i >= 0; i--) {
			history_code_data_recode.push({
				userName: data.u_name,
				code: data.history_code_data[i].code,
				pid: KPN[data.history_code_data[i].p_id] ? KPN[data.history_code_data[i].p_id] : data.history_code_data[i].p_id,
				amount: data.history_code_data[i].amount['tw'],
				time: formatTime(data.history_code_data[i].format_time)
			})
		}
		return {
			history_code_data_recode: history_code_data_recode,
			login_record_arr: login_record_arr
		};
	}

	layui.use(['form', 'layer', 'table'], function(){
		var form = layui.form;
		var layer = layui.layer;
		var table = layui.table;
		//构建表格table
		function createTable(tableData){
			table.render({
				elem:'#tableContent',
				height: 315,
				limit: 10, //分页
				page: true,
				cols: [[
					{field: 'UID', title: 'userID',fixed: 'left'},
					{field: 'userName', title: '用户名'},
					{field: 'userMail', title: '邮箱'},
					{field: 'loginIp', title: '登录地址'},
					{field: 'loginTime', title: '登录时间'}
				]],
				data:tableData.login_record_arr
			});

			table.render({
				elem:'#userCodeDataTable',
				height: 315,
				limit: 10, //分页
				page: true,
				cols: [[
					{field: 'userName', title: 'userID',fixed: 'left'},
					{field: 'code', title: '序列号'},
					{field: 'pid', title: '商品（无名称则为原始积累）'},
					{field: 'amount', title: '金额（新台币）'},
					{field: 'time', title: '激活时间'},
				]],
				data:tableData.history_code_data_recode
			});

		}
		//获取用户详情
		function getUserInfoDetail(ajaxData){
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] }); 

			$.ajax({
				url: BASE_SERVICEURL + URlS.au,
				type: 'GET',
				data: ajaxData,
				success: function(responseData){
					if(responseData.msg.code == 200){
						//console.log(responseData);
						createTable(createFormData(responseData.data));
						layer.close(loadIndex);
					}else{
						layer.msg(responseData.msg.msg, {time: 2000})
						layer.close(loadIndex);
					}
				},
				error: function(responseData){
					layer.msg(MESSAGES.service_error, {time: 2000});
					layer.close(loadIndex);

				}
			})
		}

		getUserInfoDetail(ajaxData);
	})	
})














